//
//  StockSearchResultTVC.m
//  RedManStock
//
//  Created by lanou3g on 15/7/31.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "StockSearchResultTVC.h"
#import "DataHandle.h"
#import "StockSearchResultTableViewCell.h"
#import "StockHomeViewController.h"
#import "ParameterHeader.h"
@interface StockSearchResultTVC ()<UISearchResultsUpdating,UISearchBarDelegate>

//@property(nonatomic,retain)NSMutableArray *stockArray;//用股票代码和名称拼的
//@property(nonatomic,retain)NSMutableArray *searchArray;
//@property(nonatomic,retain)UISearchController *searchController;

@end

@implementation StockSearchResultTVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    NSMutableArray *stockModalArray=[DataHandle shareHandle].allStockArray;
//    self.stockArray=[NSMutableArray array];
//    for (int i=0; i<stockModalArray.count; i++) {
//        SimpleStock *stock=stockModalArray[i];
//        [self.stockArray addObject:stock.stockStr];
//    }
    self.stockArray=[DataHandle shareHandle].allStockArray;
    self.searchArray=[NSMutableArray array];
    //设置代理
    _searchController.searchBar.delegate=self;
    _searchController.searchResultsUpdater=self;
    _searchController.searchBar.placeholder=@"请输入股票名称/代码";
    _searchController.searchBar.barTintColor=TopBarColor;
//    [_searchController.searchBar setBackgroundColor:TopBarColor];
//    _searchController.searchBar.translucent=NO;
    
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"StockSearchResultTableViewCell" bundle:nil] forCellReuseIdentifier:@"StockSearchResultTableViewCell"];
}

//-(void)searchBarButtonItemAction:(UIBarButtonItem*)rightBBI{
//    
//}

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    
    NSString *str= self.searchController.searchBar.text;
    //我擦，这里竟然可以直接用数组中模型的某个属性来做判断，醉了
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"stockStr CONTAINS [CD] %@",str];
    NSMutableArray *tempArray =[NSMutableArray arrayWithArray:[self.stockArray filteredArrayUsingPredicate:predicate]];
    [self.searchArray removeAllObjects];
    for (int i=0; i<tempArray.count; i++) {
        [self.searchArray addObject:tempArray[i]];
        if (i==8) {
            break;
        }
    }
    [self.tableView reloadData];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    self.tableView.frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 30*self.searchArray.count);
    return self.searchArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    StockSearchResultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StockSearchResultTableViewCell" forIndexPath:indexPath];
    
//    NSString *str=self.searchArray[indexPath.row];
//    //通过cell的stockInfoStr的重写的setter方法来实现赋值
//    cell.stockInfoStr=str;
    
    
    SimpleStock *stock=self.searchArray[indexPath.row];
    cell.stock=stock;
    cell.cellBlock=^(void){
        self.stockArray=[DataHandle shareHandle].allStockArray;
    };
    
// 不应该在这里修改frame，会出问题，要去numberOfRows中去改变
//    self.tableView.frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 30*self.searchArray.count);
#warning 通过数据源中simplestock的collect属性（BOOL）来改变右侧添加按钮的样式
    return cell;
}

//选中跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSString  *str=self.searchArray[indexPath.row];
//    StockHomeViewController *stockHomeVC=[[StockHomeViewController alloc]initWithHeader:NO footer:NO];
//    stockHomeVC.stockName=[str componentsSeparatedByString:@","][0];
//    stockHomeVC.stockCode=[str componentsSeparatedByString:@","][1];
//    UINavigationController *nvc=(UINavigationController *)self.presentingViewController;
    
    
    SimpleStock *stock=self.searchArray[indexPath.row];
    StockHomeViewController *stockHomeVC=[[StockHomeViewController alloc]initWithHeader:NO footer:NO];
    stockHomeVC.stockName=stock.name;
    stockHomeVC.stockCode=stock.code;
    UINavigationController *nvc=(UINavigationController *)self.presentingViewController;
    
    [self.searchController dismissViewControllerAnimated:YES completion:^{
        [nvc pushViewController:stockHomeVC animated:YES];
    }];
//    nvc.navigationBar.translucent=NO;
    
    
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
